package com.gy.smart.handle;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.gy.smart.R;
import com.gy.smart.base.BaseActivity;
import com.gy.smart.base.BaseBean;
import com.gy.smart.handle.bean.HandleDetailBean;
import com.gy.smart.handle.util.HandleDetailManager;
import com.gy.smart.util.FragmentAdapter;
import com.gy.smart.util.RequestResultListener;
import com.gy.smart.util.Urls;
import com.gy.smart.view.TitleView;

import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2017/1/5.
 * Describe:办事详情页
 */
public class HandleDetailActivity extends BaseActivity implements TitleView.TitleSelectListener {

    private TitleView youPinTitleView;
    private ViewPager viewPager;
    private FragmentAdapter fragmentAdapter;
    private List<Fragment> fragments;
    private List<String> titleList;
    private String matter_id;
    private HandleDetailBean detailBean;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.fragment_handle_detail);
        setTitleMsg("详情");
        youPinTitleView = (TitleView) findViewById(R.id.fg_handle_detail_titleview);
        viewPager = (ViewPager) findViewById(R.id.fg_handle_detail_viewpage);
    }

    @Override
    protected void initData() {
        matter_id = getIntent().getStringExtra("matter_id");
        fragments = new ArrayList<>();
        fragments.add(new HandleInforFragment());
        fragments.add(new HandleApplyFragment());
        fragments.add(new HandleConditionFragment());
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(),fragments);
        titleList = new ArrayList<>();
        titleList.add("基本信息");
        titleList.add("申报材料");
        titleList.add("受理条件");
    }

    @Override
    protected void setListener() {
        super.setListener();
        youPinTitleView.setMyListener(this);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                youPinTitleView.setSelect(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        youPinTitleView.setmDatas(titleList);
        viewPager.setAdapter(fragmentAdapter);
        getData();
    }

    @Override
    public void titleSelect(int item) {
        viewPager.setCurrentItem(item);
    }

    private void getData(){
        showLoadDialog(1);
        RequestParams params = new RequestParams(Urls.url_handle_detail);
        params.addParameter("matter_id",matter_id+"");
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                if(result.status == 1){
                    detailBean = gson.fromJson(result.data,HandleDetailBean.class);
                    HandleDetailManager.getInstance().notifyListener(detailBean);
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }


}
